/*
 * @Description:
 * @Author: Empty
 * @Date: 2019-07-19 18:01:10
 */
package singlelinkedlist

import "fmt"

/**
 * 节点
 */
type SingleListNode struct {
	next  *SingleListNode
	value interface{}
}

/**
 * 链表
 */
type SingleLinkedList struct {
	head   *SingleListNode
	length int
}

/**
 * 创建节点
 */
func NewSingleListNode(v interface{}) *SingleListNode {
	return &SingleListNode{nil, v}
}

/**
 * 创建链表
 */
func NewSingleLinkedList() *SingleLinkedList {
	return &SingleLinkedList{NewSingleListNode(0), 0}
}

/**
 * 获取节点的值
 */
func (this *SingleListNode) GetValue() interface{} {
	return this.value
}

/*
 * 获取链表长度
 */
func (this *SingleLinkedList) GetLength() interface{} {
	return this.length
}

/**
 * 获取链表头
 */
func (this *SingleLinkedList) GetHead() *SingleListNode {
	return this.head
}

/**
 * 获取链表尾部
 */
func (this *SingleLinkedList) GetTail() *SingleListNode {
	cur := this.head
	for nil != cur.next {
		cur = cur.next
	}
	return cur
}

/**
 * 获取下一个节点
 */
func (this *SingleListNode) GetNext() *SingleListNode {
	return this.next
}

/**
 * 在链表指定节点前添加节点
 */
func (this *SingleLinkedList) InsertBefore(p *SingleListNode, v interface{}) bool {
	if nil == p {
		return false
	}

	cur := this.head.next
	pre := this.head
	for nil != cur {
		if p == cur {
			break
		}
		pre = cur
		cur = cur.next
	}

	if nil == cur {
		return false
	}

	newNode := NewSingleListNode(v)
	pre.next = newNode
	newNode.next = cur
	this.length++
	return true
}

/**
 * 在链表指定节点后添加节点
 */
func (this *SingleLinkedList) InsertAfter(p *SingleListNode, v interface{}) bool {
	if nil == p {
		return false
	}

	newNode := NewSingleListNode(v)
	newNode.next = p.next
	p.next = newNode
	this.length++
	return true
}

/**
 * 在链表头部插入节点
 */
func (this *SingleLinkedList) InsertToHead(v interface{}) bool {
	return this.InsertAfter(this.head, v)
}

/**
 * 在链表尾部插入节点
 */
func (this *SingleLinkedList) InsertToTail(v interface{}) bool {
	return this.InsertAfter(this.GetTail(), v)
}

/**
 * 通过下标输出值
 */
func (this *SingleLinkedList) FindByIndex(index int) *SingleListNode {

	if index >= this.length {
		return nil
	}

	cur := this.head.next
	for i := 0; i < index; i++ {
		cur = cur.next
	}

	return cur
}

/**
 * 通过值找出节点
 */
func (this *SingleLinkedList) FindByValue(v interface{}) *SingleListNode {

	for cur := this.head.next; cur != nil; cur = cur.next {
		if cur.value == v {
			return cur
		}
	}

	return nil
}

/**
 * 删除指定节点
 */
func (this *SingleLinkedList) DeleteSingleListNode(p *SingleListNode) bool {
	if nil == p {
		return false
	}

	prev := this.head
	cur := this.head.next

	for nil != cur {
		if p == cur {
			break
		}
		prev = prev.next
		cur = cur.next
	}

	if nil == cur {
		return false
	}

	prev.next = cur.next
	this.length--
	return true
}

// 单链表反转
func (this *SingleLinkedList) SingleLinkedListReverse() {

	if nil == this.head || nil == this.head.next || nil == this.head.next.next {
		return
	}

	var pre *SingleListNode = nil
	cur := this.head.next
	for nil != cur {
		tmp := cur.next
		cur.next = pre
		pre = cur
		cur = tmp
	}

	this.head.next = pre
}

// 正序打印列表
func (this *SingleLinkedList) PrintSingleLinkedListAsort() {
	for cur := this.head.next; cur != nil; cur = cur.next {
		fmt.Printf("%+v", cur.value)
		if cur.next != nil {
			fmt.Printf("->")
		}
	}
	fmt.Println()
}
